package org.apache.ant.extensions.notify;

import java.io.IOException;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;

public class TriggerTask extends Task {

	private String watchDir = "src/main/java";
	private String targetTask = "";

	public void init() {
		super.init();
	}

	public void setWatchDir(String watchDir) {
		log("setting Directory to watch " + watchDir);
		this.watchDir = watchDir;
	}

	public void setTargetTask(String targetTask) {
		log("Will trigger task : " + targetTask);
		this.targetTask = targetTask;
	}

	public void setDaemon(boolean daemon) {
	}

	public void execute() {
		if (targetTask.trim().equals("")) {
			String msg = "targetTask can not be empty";
			throw new BuildException(msg);
		}
		FileEventsHandler handler = new TaskTriggerHandle(getProject(),
				targetTask);
		WatchDir myWatchDir;
		try {
			myWatchDir = new WatchDir(handler);
			myWatchDir.registerAll(this.watchDir);
			myWatchDir.run();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
